import Vue from 'vue';
import Vuex from 'vuex';
import { setUserCookie, getUserCookie, deleteUserCookie } from '@/utils/userCookie';

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        // collapsed 决定左侧菜单栏是否折叠，默认false不折叠
        collapsed: false,
        // 用户信息
        // user: {
        //     appkey: "",
        //     email: "",
        //     role: "",
        //     username: "",
        // }
        user: getUserCookie(),
        // 存储不同角色的路由
        menuRoutes: []
    },
    mutations: {
        // 设置菜单栏折叠
        changeCollapsed(state) {
            state.collapsed = !state.collapsed;
        },
        // 设置用户信息
        setUserInfo(state, payload) {
            state.user = payload;
        },
        // 退出登录
        logout(state) {
            state.user = {
                appkey: '',
                email: '',
                role: '',
                username: '',
            };
        },
        // 存储不同角色的路由
        setMenuRoutes(state, payload) {
            state.menuRoutes = payload;
        }
    },
    actions: {
        // 设置菜单栏折叠
        changeCollapsed(ctx) {
            ctx.commit('changeCollapsed');
        },
        // 设置用户信息
        setUserInfo(ctx, payload) {
            ctx.commit('setUserInfo', payload);
            setUserCookie(payload);
        },
        // 退出登录
        logout(ctx) {
            ctx.commit('logout');
            deleteUserCookie();
        },
        // 存储不同角色的路由
        setMenuRoutes(ctx, payload) {
            ctx.commit("setMenuRoutes", payload)
        }
    },
    modules: {
    },
});
